<?php
class DB{
		public $host;
		public $user;
		public $pwd;
		public $dbname;
		public $charset;
		public $linkId;
		/*
		构造函数
		用于类的初始化
		连接数据库，切换数据库，调整数据库编码
		*/
	   function __construct($dbhost,$dbuser,$dbpwd,$dbname,$dbcharset){
		    $this->host=$dbhost;
			$this->user=$dbuser;
			$this->pwd=$dbpwd;
			$this->dbname=$dbname;
			$this->charset=$dbcharset;
			$this->connect();
			$this->selectdb();
			$this->setcharset();
	   }
	
	   function connect(){
			$this->linkId=mysql_connect($this->host,$this->user,$this->pwd);
	   }
	   function selectdb(){
			mysql_select_db($this->dbname);
		}
		function setcharset(){
			mysql_query('set names '.$this->charset);
		}
		
		function select($table,$where='1=1',$limit='',$field=' * ',$order=''){
			$sql="select $field from $table where $where";
			if($order!=''){
				$sql.="  order by $order";
			}
			if($limit!=''){
				$sql.=" limit $limit";
			}
			$result=mysql_query($sql);
			if(mysql_num_rows($result)<=0){ return false;}
			while($row=mysql_fetch_assoc($result)){
				$ret[]=$row;
			}  
			return $ret;
		}
		
	 /*function select($table,$where='1=1'){
			if(!$this->checktable($tablename)) return false;
			$editsql="select * from $table where $where";
			$edit=mysql_query($editsql);
			$aaa=mysql_fetch_assoc($edit);
			while($aaa)
			{
				$jieguo=$aaa;	
				$aaa=mysql_fetch_assoc($edit);
			}
			if($jieguo)
			return $jieguo;
			else
			return 0;
			}*/
			
			
		function add($tablename,$data){
			if(!$this->checktable($tablename)) return false;
			$fieldlist='';
			$valuelist='';
			foreach($data as $key=>$value){
				$fieldlist.=','.$key;
				$valuelist.=','."'".$value."'";
			}
			$fieldlist=substr($fieldlist,1);
			$valuelist=substr($valuelist,1);
			$sql="insert into $tablename($fieldlist)values($valuelist)";
			if(mysql_query($sql))
			return mysql_insert_id();
			else
			return false;
		}
		function update($table,$data,$where){
			if(!$this->checktable($table)) return false;
			if($where=='')return false;
			$updatestr='';
			foreach($data as $key=>$value){
				$updatestr.=','.$key.'='."'$value'";
			}
			$updatestr=substr($updatestr,1);
			$sql="update  $table set $updatestr where $where";
			if(mysql_query($sql)) return true;
			else 
			return false;
		}
		function delete($table,$where){
			if(!$this->checktable($table)) return false;
			$sql="delete from $table where $where";
			if(mysql_query($sql)) return true;
			else 
			return false;
		}
	   function del($table,$ids){
		   if(!$this->checktable($table)) return false;
		   $pk=$this->getpk($table);
		   if(!$pk) return false;
		  	if(!is_array($ids)){
				$sql="delete from $table where $pk=$ids";	
			}else{
				$substr=implode(',',$ids);
				$sql="delete from $table where $pk in($substr)";
			}
			if(mysql_query($sql)) return true;
			else 
			return false;
	   }
		function showtables(){
			$sql="show tables";
			$query=mysql_query($sql);
			$ret=array();
			while($row=mysql_fetch_row($query)){
				$ret[]=$row[0];
			}
			mysql_free_result($query);
			return $ret;
		}
		function showfield($table){
			if(!$this->checktable($table)) return false;
			$sql="show columns from $table";
			$query=mysql_query($sql);
			$ret=array();
			while($row=mysql_fetch_assoc($query)){
				$ret[]=$row['Field'];
			}
			mysql_free_result($query);
			return $ret;
			
		}
		final function getpk($table){
			if(!$this->checktable($table)) return false;
			$sql="show columns from $table";
			$query=mysql_query($sql);
			$pk='';
			while($row=mysql_fetch_assoc($query)){
				if($row['Key']=='PRI'){
					$pk=$row['Field'];
					mysql_free_result($query);
					return $pk;
				}
			}
			mysql_free_result($query);
			return false;
		}
		
		
		function query($sql){
			  return mysql_query($sql);
			}
			
			
		function checktable($table){
			 $tables=$this->showtables();
			 if(in_array($table,$tables)){
				return true;	
			 }else{
				return false;
			 }
		}

	}

?>